package com.nian.servlet;

import com.nian.pojo.Product;
import com.nian.service.ProductService;
import com.nian.service.impl.ProductServiceImpl;


import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/admin/product.do")
public class ProductServlet extends BaseServlet{
    ProductService productService =  new ProductServiceImpl();
    public void addProduct(HttpServletRequest req, HttpServletResponse resp){
        Product product = ProductAll(req);
        boolean result = productService.addProduct(product);
        try {
            resp.sendRedirect("./product_list.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void reProduct(HttpServletRequest req, HttpServletResponse resp){
        String id = req.getParameter("id");
        Product product = ProductAll(req);
        product.setId(id);
        boolean result = productService.reProduct(product);
        try {
            resp.sendRedirect("./product_list.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void deleteProduct(HttpServletRequest req, HttpServletResponse resp){
        String id = req.getParameter("id");
        Product product = new Product();
        product.setId(id);
        boolean result = productService.dlProduct(product);
        try {
            resp.sendRedirect("./product_list.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void upFile(){

    }
    public Product ProductAll(HttpServletRequest req){
        Product product = new Product();

        product.setName(req.getParameter("name"));
        product.setPrice(Double.valueOf(req.getParameter("price")));
        product.setCategory(req.getParameter("category"));
        product.setpNum(Integer.valueOf(req.getParameter("pNum")));
        product.setImgUrl(req.getParameter("realname"));
        product.setDescription(req.getParameter("description"));
        return  product;
    }
}
